package com.doublefish.doublefishrobot.ui.setting.service;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.setting.model.AppInfoBean;
import com.doublefish.doublefishrobot.ui.setting.model.AppInfoResponse;
import com.doublefish.doublefishrobot.utils.VersionCompareUtils;
import com.doublefish.doublefishrobot.utils.db.DataBaseApi;
import com.doublefish.doublefishrobot.utils.db.TimeStampUtils;
import com.doublefish.doublefishrobot.utils.network.OKHttpApi;

/**
 * 作者:dugaofeng on 2024/10/17 08:54
 */
public class AppInfoService {

    /*
    * client  平台 int 1.荣耀、小米、OPPO、vivo  2.百度 3.腾讯 4.360  5.华为 10.蒲公英   99.iOS
    * */
    public static void getVersion(Context context, int client, OnAppInfoServiceCallback callback) {
        if (callback == null) {
            return;
        }
        //查询数据库
        DataBaseApi.queryAppInfo(client ,new DataBaseApi.OnQueryAppInfoListner() {
            @Override
            public void onCallback(boolean suc, AppInfoBean bean) {
                if (suc) {
                    if (bean == null) {
                        //本地没有数据 去服务器读取
                        requestRemoteAppInfo(context, client, bean, callback);
                    } else {
                        if (TimeStampUtils.isIntraday(bean.getUpdateTime())) {
                            //数据一天内
                            callback.onSuccess(bean);
                        } else {
                            //超出一天 去服务器读取
                            requestRemoteAppInfo(context, client, bean, callback);
                        }
                    }
                } else {
                    //本地查询失败 去服务器读取
                    requestRemoteAppInfo(context, client, bean, callback);
                }
            }
        });
    }

    private static void requestRemoteAppInfo(Context context, int client, AppInfoBean localBean, OnAppInfoServiceCallback callback) {

        OKHttpApi.getAppVersion(context, client, new OKHttpApi.OkHttpCallback() {
            @Override
            public void onNetworkPermissionError() {
                callback.onNetworkPermissionError();
            }

            @Override
            public void onCallback(boolean success, String response) {
                if (success) {
                    AppInfoResponse bean = JSONObject.parseObject(response, AppInfoResponse.class);
                    AppInfoBean remoteBean = bean.getData();
                    if (bean.getCode() == 0) {
                        if (remoteBean != null && remoteBean.getClient() == client) {
                            if (localBean == null) {
                                callback.onSuccess(remoteBean);
                                saveAppinfo(remoteBean);
                            } else {
                                //版本号是否递增
                                int res = VersionCompareUtils.compare(localBean.getVer(), remoteBean.getVer());
                                if (res == 1) {
                                    //递增
                                    remoteBean.setTid(localBean.getTid());
                                    callback.onSuccess(remoteBean);
                                    //保存到数据库
                                    saveAppinfo(remoteBean);
                                } else {
                                    //其他
                                    callback.onSuccess(localBean);
                                }
                            }
                        } else {
                            if (localBean == null) {
                                callback.onFailure(context.getString(R.string.no_data));
                            } else {
                                callback.onSuccess(localBean);
                            }
                        }
                    } else {
                        if (localBean == null) {
                            callback.onFailure(bean.getMsg());
                        } else {
                            callback.onSuccess(localBean);
                        }
                    }
                } else  {
                    if (localBean == null) {
                        callback.onFailure(response);
                    } else {
                        callback.onSuccess(localBean);
                    }
                }
            }
        });
    }

    private static void saveAppinfo(AppInfoBean bean) {
        DataBaseApi.saveAppInfo(bean, null);
    }


    public interface OnAppInfoServiceCallback {
        void onNetworkPermissionError();

        void onSuccess(AppInfoBean bean);
        void onFailure(String err);
    }
}
